package com.jy.project.typehandler;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.jy.project.domain.Material;
import com.jy.project.vo.MaterialVO;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;

/**
 * @ClassName: QualityStatusTypeHandler
 * @Description: QualityStatusTypeHandler
 * @Author jwz
 * @Date 2021/6/22 14:09
 * @Version 1.0
 */
public class QualityStatusTypeHandler extends BaseTypeHandler<String[]> {


    @Override
    public void setNonNullParameter(PreparedStatement preparedStatement, int i, String[] integers, JdbcType jdbcType) throws SQLException {
         preparedStatement.setString(i, JSONObject.toJSONString(integers));
    }

    @Override
    public String[] getNullableResult(ResultSet resultSet, String s) throws SQLException {
        return get(resultSet.getString(s));
    }

    @Override
    public String[] getNullableResult(ResultSet resultSet, int i) throws SQLException {
        return get(resultSet.getString(i));
    }

    @Override
    public String[] getNullableResult(CallableStatement callableStatement, int i) throws SQLException {
        return get(callableStatement.getString(i));
    }

    private String[] get(String s){
        if (StringUtils.isEmpty(s)){
            return  new String[0];
        }
        String[] integers = JSON.parseObject(s, String[].class);
        return  integers;
    }

}
